<?php
namespace app\common\BLL;

use app\common\IBLL\RepositoryInterface;

abstract class Repository implements RepositoryInterface
{
    protected $model;
    public function __construct()
    {
        $this->makeModel();
    }
    abstract public function model();
    public function makeModel()
    {
        $model = model($this->model());
        return $this->model = $model;
    }

    /**
    * Find data by id
    *
    * @param       $id
    * @param array $columns
    *
    * @return mixed
    */
    public function getByID($id,$columns = ['*'])
    {
        return $this->model->field($columns)->getbyid($id);
    }
    /**
    * Find data by field and value
    *
    * @param       $field
    * @param       $value
    * @param array $columns
    *
    * @return mixed
    */
    public function getByField($field, $value, $columns = ['*'])
    {
        return $this->model
                    ->where($field,$value)
                    ->field($columns)
                    ->find();
    }
    /**
    * Find data by OR
    *
    * @param array $where
    *
    */
    public function whereOR(array $where)
    {
        $this->model = $this->model->whereOr($where);
        return $this;
    }
    /**
    * Find data by multiple fields
    *
    * @param array $where
    * @param array $columns
    *
    * @return mixed
    */
    public function findWhere(array $where, $columns = ['*'])
    {
        $res = $this->model
                    ->where($where)
                    ->column($columns);
        return $res;
    }
    /**
    * Find data by multiple fields
    *
    * @param array $where
    * @param array $columns
    *
    * @return mixed
    */
    public function findWherePage(array $where, $columns = ['*'],$row=10)
    {
        $list = $this->model
                ->where($where)
                ->field($columns)
                ->paginate($row);

        return $list;
    }
    /**
    * Save a new entity in repository
    *
    * @param array $attributes
    *
    * @return mixed
    */
    public function create(array $attributes)
    {
        try{
            $this->model->data($attributes);
            $this->model->allowField(true)->save();
            return $this->model->id;
        }
        catch (\Exception $e) {
            return false;
        }
        
    }
    /**
    * 保存多笔资料
    *
    * @param array $attributes
    *
    * @return mixed
    */
    public function saveAll(array $attributes)
    {
        try{
            $this->model->saveAll($attributes);
            return true;
        }
        catch (\Exception $e) {
            return false;
        }
        
    }
    /**
    * Update a entity in repository by id
    *
    * @param array $attributes
    * @param       $id
    *
    * @return mixed
    */
    public function update(array $attributes, $id)
    {
        try {
            $this->model
                ->allowField(true)
                ->save($attributes,['id'=>$id]);
            return true;
        } catch (\Exception $e) {
            return false;
        }

    }
    /**
    * Update or Create an entity in repository
    *
    * @throws ValidatorException
    *
    * @param array $attributes
    * @param array $values
    *
    * @return mixed
    */
    public function updateOrCreate(array $attributes, array $values = [])
    {
    }
    /**
    * Delete a entity in repository by id
    *
    * @param $id
    *
    * @return int
    */
    public function delete($id)
    {
        try{
            $this->model
                ->save(['status' => 0],['id'=>$id]);
            return true;
        }
        catch (\Exception $e) {
            return false;
        }
    }
    /**
    * Order collection by a given column
    *
    * @param string $column
    *
    * @return $this
    */
    public function order($sort)
    {
        $this->model = $this->model->order($sort);
        return $this;
    }

    /**
    * 关联模型
    *
    * @param string 
    *
    * @return $this
    */
    public function with($with)
    {
        $this->model = $this->model->with($with);
        return $this;
    }
    /**
    * 关联模型查询
    *
    * @param string 
    * @param array 
    *
    * @return $this
    */
    public function hasWhere($with,$where)
    {
        $this->model = $this->model->hasWhere($with,$where);
        return $this;
    }
}
